package com.common.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

/**
 * 圆形裁剪视图
 * 
 * <br>
 * Created 2014-5-5 下午2:47:11
 * 
 * @version
 * @author HuangYK
 * 
 * @see
 */
public class ClipCircleView extends View {

	/** 半透明背景色 */
	private final static int BG_COLOR = 0x7F000000;

	/** 圆的左右padding距离 */
	private final static int CIRCLE_PADDING = 140;

	/** 圆的大小 */
	private int mCircleSize;

	/** 圆的左侧坐标 */
	private int mCircleLeft;
	/** 圆的顶部坐标 */
	private int mCircleTop;
	/** 圆的右侧坐标 */
	private int mCircleRight;
	/** 圆的底部坐标 */
	private int mCircleBottom;

	/** 屏幕宽度 */
	private int mScreenWidth;
	/** 屏幕高度 */
	private int mScreenHeight;
	/** 图片高度 */
	private int mBitmapHeight;
	/** 系统状态栏高度 */
	private int mStatusBarHeight;

	/**
	 * 构造函数，实例化ClipCircleView
	 * 
	 * @param context
	 *            上下文
	 */
	public ClipCircleView(Context context) {
		super(context);
	}

	/**
	 * 构造函数，实例化ClipCircleView
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 */
	public ClipCircleView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	/**
	 * 构造函数，实例化ClipCircleView
	 * 
	 * @param context
	 *            上下文
	 * @param attrs
	 *            属性
	 * @param defStyle
	 *            样式
	 */
	public ClipCircleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/**
	 * 初始化背景
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:47:35
	 * 
	 * @param screenWidth
	 *            屏幕宽度
	 * @param screenHeight
	 *            屏幕高度
	 * @param statusBarHeight
	 *            状态栏高度
	 * @author : HuangYK
	 */
	public void initBg(int screenWidth, int screenHeight, int statusBarHeight) {
		/** 角度 */
		final int degress = 180;
		mScreenWidth = screenWidth;
		mScreenHeight = screenHeight;
		mStatusBarHeight = statusBarHeight;
		// 实际图片高度
		mBitmapHeight = mScreenHeight - mStatusBarHeight;

		// 圆的直径大小
		mCircleSize = mScreenWidth - CIRCLE_PADDING;

		// 圆的上下左右坐标
		mCircleLeft = (mScreenWidth - mCircleSize) / 2;
		mCircleTop = (mBitmapHeight - mCircleSize) / 2;
		mCircleRight = mCircleLeft + mCircleSize;
		mCircleBottom = mCircleTop + mCircleSize;

		// 初始化画笔
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		// 背景半透明图片
		Bitmap bgBitmap = Bitmap.createBitmap(mScreenWidth, mBitmapHeight,
				Bitmap.Config.ARGB_8888);
		/******** 画半透明背景 ********/
		Canvas bgCanvas = new Canvas(bgBitmap);
		Rect outRect = new Rect(0, 0, mScreenWidth, mBitmapHeight);
		paint.setColor(BG_COLOR);
		bgCanvas.drawRect(outRect, paint);
		bgCanvas.drawBitmap(bgBitmap, outRect, outRect, paint);
		// 回收
		bgCanvas.saveLayer(new RectF(outRect), paint, Canvas.ALL_SAVE_FLAG);
		bgCanvas.restore();
		bgCanvas = null;
		paint.reset();

		// 透明圆形
		Bitmap circleBitmap = Bitmap.createBitmap(mScreenWidth, mBitmapHeight,
				Bitmap.Config.ARGB_8888);
		Path path = new Path();
		Drawable imageDrawable;
		/******** 画透明的圆 ********/
		Canvas circleCanvas = new Canvas(circleBitmap);
		imageDrawable = new BitmapDrawable(bgBitmap);
		// 透明圆形
		paint.setColor(Color.WHITE);
		Rect circleRoundRect = new Rect(0, 0, mScreenWidth, mBitmapHeight);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Paint.Style.FILL);

		RectF circleRect = new RectF(mCircleLeft, mCircleTop, mCircleRight,
				mCircleBottom);

		// 圆路径
		path.arcTo(circleRect, 0, degress);
		path.arcTo(circleRect, degress, degress);
		path.close();
		circleCanvas.drawPath(path, paint);

		// 取交集
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
		imageDrawable.setBounds(0, 0, mScreenWidth, mBitmapHeight);
		circleCanvas.saveLayer(new RectF(circleRoundRect), paint,
				Canvas.ALL_SAVE_FLAG);
		imageDrawable.draw(circleCanvas);
		circleCanvas.restore();

		circleCanvas = null;
		paint.reset();
		path.reset();
		bgBitmap.recycle();
		imageDrawable = null;

		Bitmap circleFrameBitmap = Bitmap.createBitmap(mScreenWidth,
				mBitmapHeight, Bitmap.Config.ARGB_8888);
		/******** 画透明的圆的白色边框 ********/
		Canvas canvas = new Canvas(circleFrameBitmap);
		imageDrawable = new BitmapDrawable(circleBitmap);
		paint.setStyle(Paint.Style.STROKE);
		final int strokeWidth = 3;
		paint.setStrokeWidth(strokeWidth);
		paint.setColor(Color.WHITE);
		RectF frameRoundRect = new RectF(0, 0, mScreenWidth, mBitmapHeight);
		paint.setFlags(Paint.ANTI_ALIAS_FLAG);

		RectF frameRect = new RectF(mCircleLeft - 1, mCircleTop - 1,
				mCircleRight + 1, mCircleBottom + 1);
		// 圆路径
		path.arcTo(frameRect, 0, degress);
		path.arcTo(frameRect, degress, degress);
		path.close();
		canvas.drawPath(path, paint);

		// 取交集
		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
		imageDrawable.setBounds(0, 0, mScreenWidth, mBitmapHeight);
		canvas.saveLayer(frameRoundRect, paint, Canvas.ALL_SAVE_FLAG);
		imageDrawable.draw(canvas);
		canvas.restore();

		canvas = null;
		paint.reset();
		path.reset();
		circleBitmap.recycle();
		imageDrawable = null;
		setBackgroundDrawable(new BitmapDrawable(circleFrameBitmap));
	}

	/**
	 * 获取裁剪图片
	 * 
	 * <br>
	 * Created 2014-5-5 下午2:52:25
	 * 
	 * @param screenShoot
	 *            屏幕截图
	 * @return 裁剪后的图片
	 * @author : HuangYK
	 */
	public Bitmap getClipBitmap(Bitmap screenShoot) {
		/** 角度 */
		final int degress = 180;
		/** 初始化画笔 */
		Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
		paint.setStyle(Paint.Style.FILL);
		paint.setColor(Color.WHITE);

		// 为了裁剪出圆形区域所在的方形
		Bitmap finalBitmap = Bitmap.createBitmap(screenShoot, mCircleLeft+2,
				mCircleTop + mStatusBarHeight+2, mCircleSize-2, mCircleSize-2);

		// 圆形图片
		Bitmap clipBitmap = Bitmap.createBitmap(mCircleSize, mCircleSize,
				Bitmap.Config.ARGB_8888);

		Drawable imageDrawable = new BitmapDrawable(finalBitmap);

		Canvas canvas = new Canvas(clipBitmap);

		// 透明圆形
		RectF roundRect = new RectF(0, 0, mCircleSize, mCircleSize);

		RectF circleRect = new RectF(0, 0, mCircleSize, mCircleSize);

		// 圆路径
		Path path = new Path();
		path.arcTo(circleRect, 0, degress);
		path.arcTo(circleRect, degress, degress);
		path.close();
		canvas.drawPath(path, paint);

		paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
		imageDrawable.setBounds(0, 0, mCircleSize, mCircleSize);
		canvas.saveLayer(roundRect, paint, Canvas.ALL_SAVE_FLAG);
		imageDrawable.draw(canvas);
		canvas.restore();

		canvas = null;
		paint.reset();
		path.reset();
		imageDrawable = null;
		return clipBitmap;
	}
	
	
}
